#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <fmt/format.h>
using namespace std;

// sstream include istringstream and ostringstream for string content operation

void using_ostringstream()
{
    std::ostringstream output;
    const std::string string1{"outpuy of serval data types "};
    const std::string string2{"to an ostringstream object:"};
    const std::string string3{"\ndouble: "};
    const std::string string4{"\n int: "};

    constexpr double d{123.456};
    constexpr int i{123};

    output << string1 << string2 << string3 << d << string4 << i;
    std::cout << "output contains:" << output.str();
    output << "\nmore characters added";
    std::cout << "\n\noutput now contains:\n"
              << output.str() << "\n";
}

void using_istringstream()
{
    const std::string inputString{"Amanda test 123 4.7 A"};
    std::istringstream input{inputString};
    std::string s1;
    std::string s2;
    int i;
    double d;
    char c;

    input >> s1 >> s2 >> i >> d >> c;
    std::cout << "Items extracted from the istringstream object:\n"
              << fmt::format("{}\n{}\n{}\n{}\n{}\n", s1, s2, i, d, c);
    // attempt to read from empty stream
    if (long value; input >> value)
    {
        std::cout << fmt::format("\nlong value is:{}\n", value);
    }
    else
    {
        std::cout << fmt::format("\ninput is empty\n");
    }
}

int main()
{
    // using_ostringstream();
    using_istringstream();
    return 0;
}
